<?php 
namespace App\Http\Controllers\Admin;
use Session;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Admin\ThrottlesLogins;
use Illuminate\Foundation\Admin\AuthenticatesAndRegistersUsers;
class CatController extends Controller
{
	/**
	 * 分类添加
	 * @return [type] [description]
	 */
    public function cat_add(){
    	$cate= new \App\cate();
	   	if(!empty($_POST)){
	   		$data=[
	   			'cate_name'=>$_POST['cate_name'],
	   			'parent_id'=>$_POST['parent_id'],
	   			'cate_sort'=>$_POST['cate_sort'],
	   			'is_show'=>$_POST['is_show'],
	   			'set_index_recom'=>$_POST['set_index_recom'],
	   			'cate_intro'=>$_POST['cate_intro']
	   			];
	   		$res=$cate->insert_do($data);
	   		if($res){
                Session::forget('category');
	   			echo "<script>alert('新增成功');location.href='cat_list';</script>";
	   		}else{
	   			echo "<script>alert('新增失败');location.href='cat_add';</script>";
	   		}
	   	}else{
	   		$date=$cate->getAll();
	   		$res=$cate->getLevel($date,0,"");
	   		return view('Admin/cat/cat_add',['res'=>$res]);
	   	}
   	}
   	/**
   	 * 分类展示
   	 * @return [type] [description]
   	 */
     public function cat_list(){
     	$cate= new \App\cate();
     	$date=$cate->getAll();
	   	$res=$cate->getLevel($date,0,"");
   	   return view('Admin/cat/cat_list',['res'=>$res]);
   }
   /**
    * 分类修改
    * @param  [type] $id [description]
    * @return [type]     [description]
    */
     public function cat_edit($id){
     	$cate= new \App\cate();
     	if($_POST){
     		$date=$_POST;
     		$cate_id=$date['cate_id'];
     		$data=array(
     				'cate_name'=>$date['cate_name'],
     				'parent_id'=>$date['parent_id'],
     				'cate_sort'=>$date['cate_sort'],
     				'set_index_recom'=>$date['set_index_recom'],
     				'cate_intro'=>$date['cate_intro'],
     				'is_show'=>$date['is_show'],
     			);
     		$result=$cate->updateOne($cate_id,$data);
     		if($result){
                 Session::forget('category');
	   			return redirect('/admin/cat_list');
	   		}else{
	   			echo "<script>alert('修改失败');location.href='cat_list';</script>";
	   			// return redirect('/admin/cat_edit');
	   		}
     	}else{
     		$date=$cate->getAll();
	   		$res=$cate->getLevel($date,0,"");
     		$arr=$cate->getOne($id);
     		return view('Admin/cat/cat_edit',['arr'=>$arr[0],'res'=>$res]);
     	} 
    }
    public function cat_del($id){
    	$cate= new \App\cate();
    	$result=$cate->getField($id);
    	if($result){
             Session::forget('category');
	   		return redirect('/admin/cat_list');
	   	}else{
	   		echo "<script>alert('修改失败');location.href='cat_list';</script>";
	   	}
    }

	

   
    

}

